package com.sz.admin.ai.factory.embeddingFactory.Handler;

import com.sz.admin.ai.factory.embeddingFactory.entity.EmbeddingHandlerRequestDTO;
import com.sz.admin.ai.factory.embeddingFactory.entity.EmbeddingHandlerUser;
import com.sz.admin.ai.factory.embeddingFactory.entity.EmbeddingModelEntity;
import com.sz.admin.ai.factory.embeddingFactory.entity.EmbeddingPlatformEnum;
import com.sz.core.util.StringUtils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @描述: 嵌入模型Handler
 * @Author: TangYuan
 * @Date: 2025/2/24 15:04
 */
public interface EmbeddingHandler {
    
    /**
     * 获取EmbeddingHandler
     * @return {@link EmbeddingPlatformEnum}
     */
    EmbeddingPlatformEnum getEmbeddingPlatformEnum();
    
    /**
     * 向量化数据 -> 存向量化数据
     * @param embeddingHandlerRequestDTO 参数
     * @return {@link Boolean}
     */
    Boolean vectorizedData(EmbeddingHandlerRequestDTO embeddingHandlerRequestDTO);
    
    /**
     * 使用向量化数据
     * @param embeddingHandlerRequestDTO 基本参数
     * @param advisorSpec
     * @param textContent 文本内容
     */
    void useVectorStore(EmbeddingHandlerRequestDTO embeddingHandlerRequestDTO, Integer topK, ChatClient.AdvisorSpec advisorSpec, String textContent);
    
    /**
     * 根据模型名称获取模型的Entity
     * @param modelName 模型名称
     * @return {@link EmbeddingModelEntity}
     */
    default EmbeddingModelEntity getModelEntity(String modelName) {
        EmbeddingPlatformEnum embeddingPlatformEnum = this.getEmbeddingPlatformEnum();
        for (EmbeddingModelEntity embeddingModelEntity : embeddingPlatformEnum.getModelList()) {
            if (embeddingModelEntity.getModelName().equals(modelName)) {
                return embeddingModelEntity;
            }
        }
        return null;
    }
    
    Integer delete(EmbeddingHandlerRequestDTO embeddingHandlerRequestDTO);
    
    default List<Document> getDocumentList(List<Document> documentList, String thirdPartyName, EmbeddingHandlerUser embeddingHandlerUser, String modelName, String fileName) {
        // 处理元数据
        List<Document> splitDocumentList = new ArrayList<>();
        for (Document document : documentList) {
            
            // 元数据插入metadata
            Map<String, Object> metadata = document.getMetadata();
            metadata.put("thirdPartyName", thirdPartyName);
            metadata.put("userId", embeddingHandlerUser.getUserId());
            metadata.put("knowledgeBaseId", embeddingHandlerUser.getKnowledgeBaseId());
            metadata.put("modelName", modelName);
            metadata.put("fileName", fileName);
            
            document = Document.builder()
                    .id(document.getId())
                    .text(document.getText())
                    .media(document.getMedia())
                    .metadata(metadata)
                    .score(document.getScore())
                    .build();
            splitDocumentList.add(document);
        }
        return splitDocumentList;
    }
    
    default String generateJsonPath(Map<String, String> map) {
        StringBuilder s = new StringBuilder();
        boolean isFirst = true;
        
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            
            if (StringUtils.isNotEmpty(value)) {
                // 对 value 中的特殊字符进行转义
                String escapedValue = value.replace("\"", "\\\"");
                String jsonPath = String.format("$.%s == \"%s\"", key, escapedValue);
                
                if (!isFirst) {
                    s.append(" && "); // 添加分隔符
                }
                s.append(jsonPath);
                isFirst = false;
            }
        }
        
        return s.toString();
    }
    
}
